package com.cn.aam.checaiduo.ui.me;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;

import com.cn.aam.checaiduo.AppData;
import com.cn.aam.checaiduo.Const;
import com.cn.aam.checaiduo.R;
import com.cn.aam.checaiduo.backend.RetrofitHome;
import com.cn.aam.checaiduo.backend.observer.OnRequestListener;
import com.cn.aam.checaiduo.backend.observer.OnRequestV2Listener;
import com.cn.aam.checaiduo.backend.rx.RxSchedulers;
import com.cn.aam.checaiduo.base.BaseActivity;
import com.cn.aam.checaiduo.base.ToolBarManager;
import com.cn.aam.checaiduo.base.web.NormalWebActivity;
import com.cn.aam.checaiduo.bean.base.NormalResponseB;
import com.cn.aam.checaiduo.bean.me.MeFinanceB;
import com.cn.aam.checaiduo.bean.me.RedirectContentB;
import com.cn.aam.checaiduo.utils.MyEditTextUtils;
import com.cn.aam.checaiduo.utils.MyViewsUtils;
import com.cn.aam.checaiduo.utils.StringUtil;
import com.cn.aam.checaiduo.widget.WMToast;

import butterknife.BindView;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;

/**
 * monker on 2017/5/25.
 * 自动预约
 */

public class AutoReservationActivity extends BaseActivity {
    @BindView(R.id.amount_tv_accountBalance)
    TextView tvAccountBalance;//可用余额

    @BindView(R.id.tender_et_amount)
    EditText mEtAmount;


    private String availableBalance;


    public static Intent newInstance(Context context) {
        Intent intent = new Intent(context, AutoReservationActivity.class);
        return intent;
    }

    @Override
    protected int layoutId() {
        return R.layout.activity_appoint_amount;
    }

    @Override
    protected void initView() {
        super.initView();
        ToolBarManager.with(this, WMContent).setNavigationIcon(R.drawable.ic_nav_arrow_white)
                .setTitle("预约投资").setMenuTextContent("预约记录")
                .setMenuTextClick(new ToolBarManager.OnMenuTextClickListener() {
                    @Override
                    public void onMenuTextClick() {
                        if (MyViewsUtils.isFastDoubleClick()) return;
                        startActivity(ReservationListActivity.newInstance(mContext));
                    }
                });

    }

    @OnClick(R.id.amount_tv_appoint)
    void clickToAppoint() {
        doAutoTender();   //立即预约
    }

    @OnCheckedChanged(R.id.amount_cb_useAccountBalance)
    void checkUseAccountBalance(boolean isCheck) {
        if (isCheck) {
            mEtAmount.setText(availableBalance);
            mEtAmount.setFocusable(false);
//            mEtAmount.setEnabled(false);
        } else {
            mEtAmount.setText("");
            mEtAmount.setFocusable(true);
//            mEtAmount.setEnabled(true);
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        getAccountBalance();
    }

    /**
     * 时间：2017/6/14
     * 描述：确定预约
     */
    private void doAutoTender() {
        String amount = MyEditTextUtils.getContent(mEtAmount);
        if (TextUtils.isEmpty(amount)) {
            WMToast.normal("请输入投资金额");
            return;
        }
        if (amount.charAt(0) == '.') {
            WMToast.normal("金额格式有误");
            return;
        }

        float inputAmount = Float.parseFloat(amount);
        float availabAmount = Float.parseFloat(availableBalance);
        if (inputAmount > availabAmount) {
            WMToast.normal("余额不足,请先充值");
            startActivity(RechargeActivity.newInstance(mContext,inputAmount+""));
            return;
        }

        if (inputAmount < 1000) {
            WMToast.normal("投资金额不小于1000元");
            return;
        }

        RetrofitHome.getInstance()
                .getInternalApi()
                .autoTender(AppData.getToken(), amount)
                .compose(RxSchedulers.<RedirectContentB>toMain())
                .subscribe(new OnRequestListener<RedirectContentB>(this) {
                    @Override
                    public void onNext(RedirectContentB value) {
                        super.onNext(value);
                        if (TextUtils.isEmpty(value.getRedirect_content())) {
                            WMToast.normal(value.getMsg());
                            return;
                        }
                        startActivity(NormalWebActivity.newInstance(mContext, value.getRedirect_content(), "自动预约"));
                    }
                });
    }


    /**
     * 时间：2017/6/14
     * 描述：获取可用余额，主要是自动预约取消后预约金额退回，此界面的可用金额需要重新获取
     */
    private void getAccountBalance() {
        RetrofitHome.getInstance()
                .getInternalApi()
                .appFinance(AppData.getToken())
                .compose(RxSchedulers.<NormalResponseB<MeFinanceB>>toMain())
                .subscribe(new OnRequestV2Listener<MeFinanceB>(this) {
                    @Override
                    protected boolean isShowProgressBar() {
                        return false;
                    }

                    @Override
                    public void onNext(NormalResponseB<MeFinanceB> value) {
                        super.onNext(value);
                        if (Const.STATE_LOGIN != value.getIsLogged()) {
                            goLogin();
                            return;
                        }
                        availableBalance = value.getData().getAvailable_amount() + "";
                        availableBalance = StringUtil.dealDataEnd(availableBalance);
                        tvAccountBalance.setText("¥" + availableBalance);
                    }
                });
    }
}
